package com.chinags.system.report.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import com.chinags.common.utils.DateUtils;
import com.chinags.framework.util.ShiroUtils;
import com.chinags.system.report.domain.TJcJcbgYw;
import com.chinags.system.report.service.ITJcJcbgYwService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.chinags.common.annotation.Log;
import com.chinags.common.enums.BusinessType;

import com.chinags.common.core.controller.BaseController;
import com.chinags.common.core.page.TableDataInfo;
import com.chinags.common.core.domain.AjaxResult;
import com.chinags.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 检测报告报告原文 信息操作处理
 * 
 * @author liut
 * @date 2019-06-20
 */
@Controller
@RequestMapping("/system/tJcJcbgYw")
public class TJcJcbgYwController extends BaseController
{
    private String prefix = "system/report/tJcJcbgYw";
	
	@Autowired
	private ITJcJcbgYwService tJcJcbgYwService;
	
	@RequiresPermissions("system:tJcJcbgYw:view")
	@GetMapping()
	public String tJcJcbgYw()
	{
	    return prefix + "/tJcJcbgYw";
	}
	
	/**
	 * 查询检测报告报告原文列表
	 */
	@RequiresPermissions("system:tJcJcbgYw:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TJcJcbgYw tJcJcbgYw)
	{
		startPage();
        List<TJcJcbgYw> list = tJcJcbgYwService.selectTJcJcbgYwList(tJcJcbgYw);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出检测报告报告原文列表
	 */
	@RequiresPermissions("system:tJcJcbgYw:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TJcJcbgYw tJcJcbgYw)
    {
    	List<TJcJcbgYw> list = tJcJcbgYwService.selectTJcJcbgYwList(tJcJcbgYw);
        ExcelUtil<TJcJcbgYw> util = new ExcelUtil<TJcJcbgYw>(TJcJcbgYw.class);
        return util.exportExcel(list, "tJcJcbgYw");
    }
	
	/**
	 * 新增检测报告报告原文
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存检测报告报告原文
	 */
	@RequiresPermissions("system:tJcJcbgYw:add")
	@Log(title = "检测报告报告原文", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(TJcJcbgYw tJcJcbgYw)
	{		
		return toAjax(tJcJcbgYwService.insertTJcJcbgYw(tJcJcbgYw));
	}

	/**
	 * 修改检测报告报告原文
	 */
	@GetMapping("/edit/{textId}")
	public String edit(@PathVariable("textId") Long textId, ModelMap mmap)
	{
		TJcJcbgYw tJcJcbgYw = tJcJcbgYwService.selectTJcJcbgYwById(textId);
		mmap.put("tJcJcbgYw", tJcJcbgYw);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存检测报告报告原文
	 */
	@RequiresPermissions("system:tJcJcbgYw:edit")
	@Log(title = "检测报告报告原文", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(TJcJcbgYw tJcJcbgYw)
	{		
		return toAjax(tJcJcbgYwService.updateTJcJcbgYw(tJcJcbgYw));
	}
	
	/**
	 * 删除检测报告报告原文
	 */
	@RequiresPermissions("system:tJcJcbgYw:remove")
	@Log(title = "检测报告报告原文", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(tJcJcbgYwService.deleteTJcJcbgYwByIds(ids));
	}
	/***
	 * 导入检测报告原文信息表
	 */

	@RequiresPermissions("system:tJcJcbgYw:import")
	@PostMapping("/importData")
	@ResponseBody
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
	{
		ExcelUtil<TJcJcbgYw> util = new ExcelUtil<TJcJcbgYw>(TJcJcbgYw.class);
		List<TJcJcbgYw> tJcJcbgYwList = util.importExcel(file.getInputStream());
		String operName = ShiroUtils.getSysUser().getLoginName();
		String message = tJcJcbgYwService.importUser(tJcJcbgYwList, updateSupport, operName);
		return AjaxResult.success(message);
	}
	/**
	 * 下载模板
	 *
	 * */
	@RequiresPermissions("system:tJcJcbgYw:view")
	@GetMapping("/importTemplate")
	@ResponseBody
	public AjaxResult importTemplate()
	{
		ExcelUtil<TJcJcbgYw> util = new ExcelUtil<TJcJcbgYw>(TJcJcbgYw.class);
		return util.importTemplateExcel("检测报告原文信息数据");
	}
	/**
	 * 查看详细
	 */
	@GetMapping("/detail/{textMessageId}")
	public String detail(@PathVariable("textMessageId") Long textMessageId, ModelMap mmap)
	{
		mmap.put("name", "tJcJcbgYw");
		mmap.put("tJcJcbgYw", tJcJcbgYwService.selectJobLogById(textMessageId));
		return prefix + "/detail";
	}
	/**
	 * 保存上传文件
	 * @param id   关联的记录id
	 * @param type   文件所属类型，1.信息公示 2.监督检查 3.意见建议 4.管理制度
	 */
	@ResponseBody
	@RequestMapping(value = "/fileup")
	public String fileup(@RequestParam("file1") MultipartFile[] file1, HttpServletRequest request, HttpServletResponse response){
//		response.setContentType("application/json; charset=UTF-8");
		String nameId = UUID.randomUUID().toString();
		String repath = request.getSession().getServletContext().getRealPath("/")+"user";
		String path = "report/upload"+ DateUtils.getDate()+"/";
		File imagefile  = new File(repath+path);

		for (int i = 0; i < file1.length; i++) {
			//上传文件类型（尾缀）
			String weizhui = file1[i].getOriginalFilename().lastIndexOf(".")!=-1?file1[i].getOriginalFilename().
					substring(file1[i].getOriginalFilename().lastIndexOf(".")+1, file1[i].getOriginalFilename().length()):null;
			String name=file1[i].getOriginalFilename();  //原文件名字
			try {

				if(!imagefile.exists()){
					imagefile.mkdirs();
				}
				imagefile = new File(repath+path+nameId+"."+weizhui);
				imagefile.createNewFile();
				file1[i].transferTo(imagefile);

			}catch (IOException e){
				e.printStackTrace();
			}
			logger.info(path+"文件存储成功");
		}
		return "{\"code:\"1\"success\",\"result\":\"上传成功\",\"id\",\"test\",\"path\":\""+repath+path+nameId+"\"}";

	}




}
